<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
</head>

<body>
  <input type="text" name="" value='青岛'><button>查询城市1</button>
  <input type="text" name="" value='青岛'><button>查询城市2</button>

  <div id="main" style="width:100%;height: 400px;"></div>
  <script type="text/javascript" src="./echarts.js"></script>
  <script type="text/javascript" src="../13AJAX（尤其重要）/ajax.js"></script>
  <script type="text/javascript">
    let myChart = echarts.init(document.getElementById('main'));
    let option = {
      title: {
        text: 'Stacked Line'
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['Email', 'Union Ads']
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      toolbox: {
        feature: {
          saveAsImage: {}
        }
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: 'Email',
          type: 'line',
          stack: 'Total',
          data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
          name: 'Union Ads',
          type: 'line',
          stack: 'Total',
          data: [220, 182, 191, 234, 290, 330, 310]
        }
      ]
    };
    myChart.setOption(option)

    let btn = document.querySelectorAll('button')
    let inp = document.querySelectorAll('input')
    for (let i = 0; i < btn.length; i++) {
      btn[i].onclick = function () {
        ajax({
          method: 'get',
          url: 'https://geoapi.qweather.com/v2/city/lookup',
          params: {
            location: inp[i].value,
            key: 'c494e264764449f7b4f6bf742276d7f9'
          },
          success: function (res) {
            console.log(res)
            weather(res.location[0].id, i)
          }
        })
      }
    }


    function weather(id, val) {
      ajax({
        method: 'get',
        url: 'https://devapi.qweather.com/v7/weather/7d',
        params: {
          location: id,
          key: 'c494e264764449f7b4f6bf742276d7f9'
        },
        success: function (res) {
          console.log(res)
          if (res.code == 200) {
            option.xAxis.data = []
            option.series[val].data = []
            console.log(val)
            for (let i = 0; i < res.daily.length; i++) {
              option.xAxis.data.push(res.daily[i].fxDate)
              if (val == 0) {
                option.series[val].data.push(res.daily[i].humidity)
              } else {
                option.series[val].data.push(res.daily[i].humidity)
              }
            }
            // 重新渲染
            myChart.setOption(option)
          }
        }
      })
    }
  </script>
</body>

</html>